# 数据类型
# 整形 布尔型 浮点数 字符串 列表[] 元组() 集合{} 字典{}

# 猜数字
# import random
# number = random.randint(1, 100)
# while True:
#     guess = int(input('请输入数字：'))
#     if guess > number:
#         print('数字太大了')
#     elif guess < number:
#         print('数字太小了')
#     else:
#         print('恭喜你猜对了')
#         break

# 打印九九乘法表
# for i in range(1, 10):
#     for j in range(1, i + 1):
#         print('%d * %d = %d' % (i, j, i * j), end='\t')
#     print()

# 使用tutle画图  实现绘制正方形
# import turtle
# turtle.pencolor('red')
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.done()


# import turtle
#
# def draw_star(size, color='yellow', fill=True):
#     """
#     使用turtle绘制五角星
#
#     参数:
#     size: 五角星的大小
#     color: 五角星的颜色，默认为黄色
#     fill: 是否填充，默认为True
#     """
#     # 设置画笔颜色
#     turtle.pencolor(color)
#
#     # 如果需要填充，则设置填充颜色
#     if fill:
#         turtle.fillcolor(color)
#         turtle.begin_fill()
#
#     # 绘制五角星
#     for i in range(5):
#         turtle.forward(size)
#         turtle.right(144)
#
#     # 结束填充
#     if fill:
#         turtle.end_fill()
#
# def draw_china_flag():
#     """
#     绘制五星红旗
#     """
#     # 设置画布
#     turtle.setup(800, 500)
#     turtle.bgcolor('red')
#     turtle.speed(5)
#
#     # 绘制大五角星（位于左上角）
#     turtle.penup()
#     turtle.goto(-300, 150)
#     turtle.pendown()
#     draw_star(50, 'yellow', True)
#
#     # 绘制四个小五角星
#     # 第一个小五角星
#     turtle.penup()
#     turtle.goto(-200, 200)
#     turtle.pendown()
#     turtle.setheading(turtle.towards(-300, 150))  # 朝向大五角星
#     turtle.right(30)  # 调整角度使五角星有一个角指向大五角星
#     draw_star(20, 'yellow', True)
#
#     # 第二个小五角星
#     turtle.penup()
#     turtle.goto(-160, 160)
#     turtle.pendown()
#     turtle.setheading(turtle.towards(-300, 150))
#     turtle.right(20)
#     draw_star(20, 'yellow', True)
#
#     # 第三个小五角星
#     turtle.penup()
#     turtle.goto(-160, 100)
#     turtle.pendown()
#     turtle.setheading(turtle.towards(-300, 150))
#     turtle.right(10)
#     draw_star(20, 'yellow', True)
#
#     # 第四个小五角星
#     turtle.penup()
#     turtle.goto(-200, 60)
#     turtle.pendown()
#     turtle.setheading(turtle.towards(-300, 150))
#     turtle.right(0)
#     draw_star(20, 'yellow', True)
#
#     # 隐藏画笔并完成绘制
#     turtle.hideturtle()
#     turtle.done()


# print("zjh")

import  pygame
# 使用pygame模块
# 实现植物大战僵尸中僵尸动态行走的代码
# 图片路径:images/普通/1.png到22.png共22张图片
# import pygame
# import os
# import sys
#
# # 初始化pygame
# pygame.init()
#
# # 设置窗口大小
# SCREEN_WIDTH, SCREEN_HEIGHT = 1000, 600
# screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# pygame.display.set_caption("Plants vs Zombies - Zombie Walk")
#
# # 加载僵尸行走动画帧
# zombie_images = []
# for i in range(1, 23):  # images/普通/1.png 到 images/普通/22.png
#     img_path = os.path.join("images", "普通", f"{i}.png")
#     if os.path.exists(img_path):
#         image = pygame.image.load(img_path).convert_alpha()
#         zombie_images.append(image)
#     else:
#         print(f"警告: 找不到图片 {img_path}")
#         # 如果找不到图片，使用一个替代的表面
#         zombie_images.append(pygame.Surface((50, 80)))
#         zombie_images[-1].fill((255, 0, 0))  # 红色矩形作为替代
#
# # 检查是否有加载到图片
# if not zombie_images:
#     print("错误: 没有加载到任何僵尸动画帧")
#     pygame.quit()
#     sys.exit()
#
# # 游戏变量
# zombie_x = SCREEN_WIDTH  # 僵尸起始位置在屏幕右侧
# zombie_y1 = SCREEN_HEIGHT - 150  # 第一个僵尸垂直位置
# zombie_y2 = SCREEN_HEIGHT - 300  # 第二个僵尸垂直位置
# zombie_y3 = SCREEN_HEIGHT - 450  # 第三个僵尸垂直位置
# zombie_speed = 2  # 僵尸行走速度
# current_frame = 0  # 当前动画帧索引
# frame_rate = 10  # 动画帧率 (FPS)
# clock = pygame.time.Clock()
#
# # 主游戏循环
# running = True
# while running:
#     # 处理事件
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:
#             running = False
#
#     # 更新僵尸位置
#     zombie_x -= zombie_speed
#     # 如果僵尸走出屏幕左侧，重置到右侧
#     if zombie_x < -100:
#         zombie_x = SCREEN_WIDTH
#
#     # 更新动画帧
#     current_frame = (current_frame + 1) % len(zombie_images)
#
#     # 绘制
#     screen.fill((135, 206, 235))  # 填充天空蓝背景
#     # 绘制三个垂直排列的僵尸图像
#     screen.blit(zombie_images[current_frame], (zombie_x, zombie_y1))
#     screen.blit(zombie_images[current_frame], (zombie_x, zombie_y2))
#     screen.blit(zombie_images[current_frame], (zombie_x, zombie_y3))
#
#     # 更新显示
#     pygame.display.flip()
#
#     # 控制帧率
#     clock.tick(frame_rate)
#
# # 退出pygame
# pygame.quit()
# sys.exit()


# 使用pygame模块
# 实现植物大战僵尸中向日葵动态的代码
# 图片路径:images/向日葵/1.png到18.png共18张图片
# import pygame
# import os
# import sys
#
# # 初始化pygame
# pygame.init()
#
# # 设置窗口大小
# SCREEN_WIDTH, SCREEN_HEIGHT = 1000, 600
# screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# pygame.display.set_caption("Plants vs Zombies - Sunflower Animation")
#
# # 加载向日葵动画帧
# sunflower_images = []
# for i in range(1, 19):  # images/向日葵/1.png 到 images/向日葵/18.png
#     img_path = os.path.join("images", "向日葵", f"{i}.png")
#     if os.path.exists(img_path):
#         image = pygame.image.load(img_path).convert_alpha()
#         sunflower_images.append(image)
#     else:
#         print(f"警告: 找不到图片 {img_path}")
#         # 如果找不到图片，使用一个替代的表面
#         sunflower_images.append(pygame.Surface((80, 80)))
#         sunflower_images[-1].fill((255, 255, 0))  # 黄色矩形作为替代
#
# # 检查是否有加载到图片
# if not sunflower_images:
#     print("错误: 没有加载到任何向日葵动画帧")
#     pygame.quit()
#     sys.exit()
#
# # 游戏变量
# sunflower_x = SCREEN_WIDTH // 2 - 40  # 向日葵水平居中位置
# sunflower_y = SCREEN_HEIGHT // 2 - 40  # 向日葵垂直居中位置
# current_frame = 0  # 当前动画帧索引
# frame_rate = 9# 动画帧率 (FPS)，可调整以改变动画速度
# clock = pygame.time.Clock()
#
# # 主游戏循环
# running = True
# while running:
#     # 处理事件
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:
#             running = False
#
#     # 更新动画帧
#     current_frame = (current_frame + 1) % len(sunflower_images)
#
#     # 绘制
#     screen.fill((135, 206, 235))  # 填充天空蓝背景
#     # 绘制当前帧的向日葵图像
#     screen.blit(sunflower_images[current_frame], (sunflower_x, sunflower_y))
#
#     # 更新显示
#     pygame.display.flip()
#
#     # 控制帧率
#     clock.tick(frame_rate)
#
# # 退出pygame
# pygame.quit()
# sys.exit()

# 使用pygame模块
# 实现锤子砸僵尸的积分游戏一个僵尸10积分的代码
# 僵尸图片路径:images/普通/1.png到22.png共22张图片
# 锤子图片路径:images/hammer.png
# 背景图片路径:images/beijing.jpg
import pygame
import os
import sys
import random

# 初始化pygame
pygame.init()

# 设置窗口大小
SCREEN_WIDTH, SCREEN_HEIGHT = 1000, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("锤子砸僵尸积分游戏")

# 加载背景图片
background_image = None
bg_path = os.path.join("images", "beijing.jpg")
if os.path.exists(bg_path):
    background_image = pygame.image.load(bg_path).convert()
    background_image = pygame.transform.scale(background_image, (SCREEN_WIDTH, SCREEN_HEIGHT))
else:
    print(f"警告: 找不到背景图片 {bg_path}")

# 加载锤子图片
hammer_image = None
hammer_path = os.path.join("images", "hammer.png")
if os.path.exists(hammer_path):
    hammer_image = pygame.image.load(hammer_path).convert_alpha()
    hammer_image = pygame.transform.scale(hammer_image, (40, 40))
else:
    print(f"警告: 找不到锤子图片 {hammer_path}")
    # 创建默认锤子
    hammer_image = pygame.Surface((40, 40))
    hammer_image.fill((139, 69, 19))  # 棕色

# 加载奖杯图片
trophy_image = None
trophy_path = os.path.join("images", "trophy_hi_res.png")
if os.path.exists(trophy_path):
    trophy_image = pygame.image.load(trophy_path).convert_alpha()
    trophy_image = pygame.transform.scale(trophy_image, (200, 200))
else:
    print(f"警告: 找不到奖杯图片 {trophy_path}")

# 加载失败图片
fail_image = None
fail_path = os.path.join("images", "ZombiesWon.jpg")
if os.path.exists(fail_path):
    fail_image = pygame.image.load(fail_path).convert_alpha()
    fail_image = pygame.transform.scale(fail_image, (200, 200))
else:
    print(f"警告: 找不到失败图片 {fail_path}")

# 加载小车图片
car_image = None
car_path = os.path.join("images", "车.png")
if os.path.exists(car_path):
    car_image = pygame.image.load(car_path).convert_alpha()
    # 调整小车图片大小
    car_image = pygame.transform.scale(car_image, (60, 60))
else:
    print(f"警告: 找不到小车图片 {car_path}")
    # 创建默认小车
    car_image = pygame.Surface((60, 60))
    car_image.fill((0, 255, 0))  # 绿色矩形作为替代

# 加载僵尸行走动画帧
zombie_images = []
for i in range(1, 23):  # images/普通/1.png 到 images/普通/22.png
    img_path = os.path.join("images", "普通", f"{i}.png")
    if os.path.exists(img_path):
        image = pygame.image.load(img_path).convert_alpha()
        zombie_images.append(image)
    else:
        print(f"警告: 找不到图片 {img_path}")
        # 如果找不到图片，使用一个替代的表面
        zombie_images.append(pygame.Surface((50, 80)))
        zombie_images[-1].fill((255, 0, 0))  # 红色矩形作为替代

# 检查是否有加载到图片
if not zombie_images:
    print("错误: 没有加载到任何僵尸动画帧")
    pygame.quit()
    sys.exit()


# 定义僵尸类
class Zombie:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 50
        self.height = 80
        self.speed = random.randint(1, 3)
        self.current_frame = 0
        self.animation_speed = 5
        self.animation_counter = 0
        self.alive = True
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        if self.alive:
            # 更新位置
            self.x -= self.speed
            # 更新动画
            self.animation_counter += 1
            if self.animation_counter >= self.animation_speed:
                self.current_frame = (self.current_frame + 1) % len(zombie_images)
                self.animation_counter = 0

            # 更新rect位置
            self.rect.x = self.x
            self.rect.y = self.y

            # 如果僵尸走出屏幕左侧，标记为死亡
            if self.x < 0:  # 修改判定条件为 < 0
                self.alive = False

    def draw(self):
        if self.alive:
            screen.blit(zombie_images[self.current_frame], (self.x, self.y))

    def hit(self, pos):
        """检查是否被点击"""
        if self.alive and self.rect.collidepoint(pos):
            self.alive = False
            return True
        return False


# 游戏变量
zombies = []  # 僵尸列表
score = 0  # 积分
zombies_killed = 0  # 击杀的僵尸数量
font = pygame.font.Font(None, 36)  # 字体
spawn_timer = 0  # 僵尸生成计时器
spawn_delay = 120  # 僵尸生成延迟（帧）- 降低出现频率
frame_rate = 60  # 帧率
clock = pygame.time.Clock()

# 小车位置参数
car_positions = [
    (20, 100),
    (20, 200),
    (20, 300),
    (20, 400),
    (20, 500)
]

# 游戏控制变量
game_over = False  # 游戏结束标志
game_result = ""  # 游戏结果 ("win" 或 "lose")

# 主游戏循环
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            # 鼠标点击事件
            if event.button == 1:  # 左键点击
                # 检查是否击中僵尸
                for zombie in zombies:
                    if zombie.hit(event.pos):
                        score += 10  # 击中僵尸得10分
                        zombies_killed += 1  # 增加击杀数
                        break  # 一次只能击中一个僵尸

    if not game_over:
        # 生成新僵尸
        spawn_timer += 1
        if spawn_timer >= spawn_delay:
            spawn_timer = 0
            # 随机Y位置生成僵尸
            y_pos = random.randint(100, SCREEN_HEIGHT - 100)
            zombies.append(Zombie(SCREEN_WIDTH, y_pos))

        # 更新僵尸
        for zombie in zombies[:]:  # 使用副本列表防止迭代时修改
            zombie.update()
            if not zombie.alive:
                # 检查僵尸是否是因为走到最左侧而死亡
                if zombie.x < 0:
                    game_over = True
                    game_result = "lose"
                zombies.remove(zombie)

        # 检查是否击杀了10个僵尸
        if zombies_killed >= 10:
            game_over = True
            game_result = "win"

    # 绘制
    # 绘制背景
    if background_image:
        screen.blit(background_image, (0, 0))
    else:
        screen.fill((135, 206, 235))  # 填充天空蓝背景

    # 绘制小车列
    if car_image:
        for pos in car_positions:
            screen.blit(car_image, pos)

    # 绘制僵尸
    for zombie in zombies:
        zombie.draw()

    # 绘制锤子 - 直接使用鼠标位置
    mouse_pos = pygame.mouse.get_pos()
    screen.blit(hammer_image, (mouse_pos[0] - 20, mouse_pos[1] - 20))  # 调整偏移量使锤子尖端对准鼠标

    # 绘制积分和击杀数
    text_surface = font.render(f"jifen: {score}", True, (255, 255, 255))
    killed_text = font.render(f"jisha: {zombies_killed}/10", True, (255, 255, 255))
    screen.blit(text_surface, (10, 10))
    screen.blit(killed_text, (10, 50))

    # 绘制游戏结束信息
    if game_over:
        if game_result == "win":
            # 显示奖杯图片
            if trophy_image:
                screen.blit(trophy_image, (SCREEN_WIDTH // 2 - trophy_image.get_width() // 2, SCREEN_HEIGHT // 2 - 100))
        else:
            # 显示失败图片
            if fail_image:
                screen.blit(fail_image, (SCREEN_WIDTH // 2 - fail_image.get_width() // 2, SCREEN_HEIGHT // 2 - 100))

        # 显示最终积分
        final_score_text = font.render(f"jifen: {score}", True, (255, 255, 255))
        screen.blit(final_score_text, (SCREEN_WIDTH // 2 - final_score_text.get_width() // 2, SCREEN_HEIGHT // 2 + 120))

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    clock.tick(frame_rate)

# 退出pygame
pygame.quit()
sys.exit()





